$(document).ready(function() {
		var genre = "";
		
			
			$( "input[type='button']").button();
			$("#ac_form").hide();
			$("#ec_form").hide();
			$("#prevPage").hide();
			$(".success").hide();
			$(".error").hide();
			$("#nextPage").live('click', function(){
				var index = $("#index").val();
				var addedIndex = parseInt(index) + 10;
				$("#index").val(addedIndex);
				var max = $("#count").val();
				if(addedIndex+10 >= parseInt(max))
				{
					$("#nextPage").hide();
					$("#prevPage").show();
				}
				$.post("/Book/filtersearchedBooks",{ 
							"index": addedIndex							
							},function(data){ 
									var content = "";
								$.each(data, function( i, books){
									content += "<tr class=\"book_row\" id=\""+books.isbn+"_rows\">";
									content += "<td id=\""+books.book_ID+"_id\">"+books.isbn+"</td>";
									content += "<td id=\""+books.book_ID+"_title\">"+books.title+"</td>";
									content += "<td id=\""+books.book_ID+"_author\">"+books.author+"</td>";
									content += "<td id=\""+books.book_ID+"_publisher\">"+books.publisher+"</td>";
									content += "</tr>";
									
								});
								$("#sb_tbdy").html(content);
								
							},"json");
							
							return false;
			
			});
			
			$("#prevPage").live('click', function(){
				var index = $("#index").val();
				var addedIndex = parseInt(index) - 10;
				$("#index").val(addedIndex);
				if(addedIndex-10 <= 0)
				{
					$("#prevPage").hide();
					$("#nextPage").show();
				}
				$.post("/Book/filtersearchedBooks",{ 
							"index": addedIndex							
							},function(data){ 
									var content = "";
								$.each(data, function( i, books){
									content += "<tr class=\"book_row\" id=\""+books.isbn+"_rows\">";
									content += "<td id=\""+books.book_ID+"_id\">"+books.isbn+"</td>";
									content += "<td id=\""+books.book_ID+"_title\">"+books.title+"</td>";
									content += "<td id=\""+books.book_ID+"_author\">"+books.author+"</td>";
									content += "<td id=\""+books.book_ID+"_publisher\">"+books.publisher+"</td>";
									content += "</tr>";
									
								});
								$("#sb_tbdy").html(content);
								
							},"json");
							
							return false;
			
			});
			
			$("#showACForm").click(function(){
				$("#ac_form").slideDown();
			
			});
			
			$(".showECForm").click(function(){
				var copy_id = $(this).attr("id");
				$("#ec_copy_id").val(copy_id);
				var element = "#"+copy_id+"_inp_call_no";
				var callNo = $(element).html();
				$("#ec_callNo").val(callNo);
				$("#ec_form").slideDown();
			
			});
			
			$("#editCopy").click(function(){
				var copy_id = $("#ec_copy_id").val();
				var callNo = $("#ec_callNo").val();
				
				if(callNo != "")
				{
						$.post("/Book/EditCopy",{
							    "copy_id": copy_id,
							    "callNo": callNo
					},function(data){
							location.reload();	
									
						});
				}
				else
				{
					$(".error").html("Please fill the call Number").slideDown();
				}
				
			
			});
			
			
			$("#addCopy").click(function(){
				var ISBN = $("#ac_ISBN").val();
				var callNo = $("#ac_callNo").val();
				
				if(callNo != "")
				{
						$.post("/Book/AddCopy",{
							    "isbn": ISBN,
							    "callNo": callNo
					},function(data){
							location.reload();	
									
						});
				}
				else
				{
					$(".error").html("Please fill the call Number").slideDown();
				}
			
			});
			
			$(".saveReservation").click(function(){
				var date = $("#datepicker").val();
				var dummy = $("#latest_reservation").html();
				var latest_reservation_date = new Date( dummy );
				var copy_id = $(this).attr("id");
				var currentDate = new Date();
				var resDate = new Date(date);
				
				if(date == "")
				{
					$(".error").html("Please choose a date!").slideDown();
				}
				else
				{
					$(".error").slideUp();
					$.post("/Book/saveReservation",{
							    "copy_id": copy_id,
							    "date": date
							},function(data){
								location.reload();	
									
								});
				}
				return false;
			})
			
			$(".checkbox").click(function(){
				genre ="";
				
				$("input:checked").each(function(){
					genre += $(this).attr("name")+", ";
					
					});
				genre = genre.substring(0, genre.length -2);
				$("#ab_genre").val(genre);
			});
			
			$("input:checked").each(function(){
					genre += $(this).attr("name")+", ";
					
					});
			genre = genre.substring(0, genre.length -2);
			$("#ab_genre").val(genre);
			
			// -----------------------------------------------------------  LOOKUP BY ISBN --------------------------------------------------------------//
			$("#lookup_isbn").click(function(){
					var t_isbn = $("#ab_isbn").val();
					
					if(t_isbn == '')
					{
						$("#ab_error").html("Cannot lookup book! Please fill up ISBN information").slideDown();
					}
					else
					{
					
						$(".error").slideUp();
						$("#ab_success").html("Please wait, looking up ISBN in amazon.com").slideDown(); 						    
						console.log("Check Point 1");
						$.post("/Book/LookupAWSByISBN",{ 
							"isbn": t_isbn,
							},function(data){ 
								console.log("Check Point 2");
								$("#ab_success").slideUp(); 						    
								if(data == 1) {
									$("#ab_error").html("Couldn't connect to amazon.com, please check your internet connection.").slideDown();
								}else if(data == 2) {
									$("#ab_error").html("Generated result was too long.<br/>Are you sure you've entered the correct ISBN.").slideDown();
								}else if(data == 3) {
									$("#ab_error").html("Search Result Page returned empty.<br/>Maximum Search Result Page No. exceeded.").slideDown();
								}else {
									$("#ab_success").html("amazon.com lookup completed").slideDown(800).delay(800).slideUp(); 						    
									$("#ab_isbn").val(data.ISBN); 						    
									$("#ab_title").val(data.Title); 						    
									$("#ab_author").val(data.Author[0]); 						    
									$("#ab_publisher").val(data.Publisher);
									$("#ab_callNo").val("");
									$("#ab_synopsis").val(data.ShortSynopsis); 						    
									$("#ab_image").val(data.LargeImage.URL);  			
								}
								$("#ab_success").html("Book Added!");
							},"json");
					}
				});
				
			// -----------------------------------------------------------  ADD BOOK --------------------------------------------------------------//
			$("#add_book").click(function(){
					var t_isbn = $("#ab_isbn").val();
					var t_title = $("#ab_title").val();
					var t_author = $("#ab_author").val();
					var t_publisher = $("#ab_publisher").val();
					var t_synopsis = $("#ab_synopsis").val();
					var t_image = $("#ab_image").val();
					var t_genre = $("#ab_genre").val();
					var t_callNo = $("#ab_callNo").val();
					var imageSize = 0;
					var extension = "jpg";
					var fileInput = "";
					
					if(t_image != "")
					{
						
						fileInput = $("#ab_image")[0];
						imageSize = fileInput.files[0].fileSize/1024; 
						extension = t_image.substr( (t_image.lastIndexOf('.') +1) );
					}
					
					if(t_title == '' || t_author == '' || t_publisher == '' || t_callNo == '')
					{
						$("#ab_error").html("Cannot add book! Please fill up all the necessary information").slideDown();
					}
					else if(imageSize != 0 && imageSize > 2000 )
					{
						$("#ab_error").html("Image size cannot exceed 2MB").slideDown();
					}
					else if(imageSize != 0 && extension != "jpg")
					{
						$("#ab_error").html("Image must be a jpg file").slideDown();
					}
					else
					{
					
						$.post("/Book/AddBook",{ 
							"isbn": t_isbn,						    
							"title": t_title, 						    
							"author": t_author, 
							"image": t_image,
							"synopsis": t_synopsis,					    
							"publisher": t_publisher,
							"genre": t_genre,
							"callNo": t_callNo						
							},function(data){ 
								if(data > 0)
								{
									$("#ab_error").html("Cannot Add Book! Existing ISBN <br/> Do you want to add a copy instead?<br/><a href=\"#\">Yes</a>").slideDown();
									
									
								}
								else
								{
									$("#sb_tbdy").slideDown().append("	<tr class=\"book_row\" id=\""+data.isbn+"_rows\" >"+
										"<td id=\""+data.book_ID+"_id\">"+  data.isbn +"</td>" +
										"<td id=\""+data.book_ID+"_title\">"+  data.title +"</td>" +
										"<td id=\""+data.book_ID+"_author\">"+  data.author +"</td>" +
										"<td id=\""+data.book_ID+"_publisher\">"+  data.publisher +"</td>" +
									"</tr>");
											 		    		 		    
									$(".error").slideUp(); 						    
									$("#ab_success").slideDown(800).delay(800).slideUp(); 						    
									$("#ab_title").val(""); 						    
									$("#ab_author").val(""); 						    
									$("#ab_publisher").val("");
									$("#ab_callNo").val("");
									$("#ab_isbn").val(""); 						    
									$("#ab_synopsis").val(""); 						    
									$("#ab_image").val("");  			
								}
											
							},"json");
						
						
						
					}
				
				});
				
				// ------------------------------------------------- SEARCH BOOK --------------------------------------------------------//
				
			/*																			
				$("#s_book").keyup(function(){
					var search = $("#s_book").val();
					$.post("../Book/SearchBook",{
						    "search": search
						},function(data){
							var content = "";
							
							$.each(data, function( i, books){
								content += "<tr class=\"book_row\" id=\""+books.isbn+"_rows\">";
								content += "<td>"+books.isbn+"</td>";
								content += "<td>"+books.title+"</td>";
								content += "<td>"+books.author+"</td>";
								content += "<td>available</td>";
								content += "</tr>";
								
							});
							$("#sb_tbdy").html(content);
						}, "JSON");
				});*/
				
																						
				$("#sb_book").live('keyup',function(){
						var max = $("#count").val();
						console.log(max);
						
				
						var search = $("#sb_book").val();
						$.post("../Book/SearchBook",{
							    "search": search
							},function(data){
								$("#count").val(data.length);
								if(data.length <= 10)
								{
									$("#nextPage").hide();
								}
								else
								{
									$("#nextPage").show();
								}
								
								var content = "";
								$.each(data, function( i, books){
									content += "<tr class=\"book_row\" id=\""+books.isbn+"_rows\">";
									content += "<td id=\""+books.book_ID+"_id\">"+books.isbn+"</td>";
									content += "<td id=\""+books.book_ID+"_title\">"+books.title+"</td>";
									content += "<td id=\""+books.book_ID+"_author\">"+books.author+"</td>";
									content += "<td id=\""+books.book_ID+"_publisher\">"+books.publisher+"</td>";
									content += "</tr>";
									
								});
								$("#sb_tbdy").html(content);
							}, "JSON");
				});
			
				
				// ------------------------------------------------- DELETE BOOK --------------------------------------------------------//
				$(".deleteBook").live('click',function(){
					var id = $(this).attr("id");		
					
					$.post("/Book/deleteBook",{
						    "book_id": id
						},function(data){
							//$("#db_success").slideDown(800).delay(800).slideUp();
						   // $(".error").slideUp();
						    //$(row).fadeOut();
						    window.location.replace("/Book/Manage/");
						});
						
				});
				
				// ------------------------------------------------- EDIT BOOK --------------------------------------------------------//
				$(".editBook_showForm").live('click',function(){
					$("#form_editBook").slideDown();
					
					return false;
					
				});
				
				$("#cancel_editBook").click(function(){
					$("#form_editBook").slideUp();
				});
				
				$("#editBook").live('click',function(){
					var id = $("#inp_id").val();
					var isbn = $("#inp_isbn").val();
					var title = $("#inp_title").val();
					var author = $("#inp_author").val();
					var publisher = $("#inp_publisher").val();
					var genre = $("#ab_genre").val();
					var synopsis = $("#inp_synopsis").val();					
					
					if(id != "" || title != "" || author != "" || subject != "")
					{
						$.post("/Book/editBook",{
							"book_id": id,
						    "isbn": isbn,
						    "title": title,
						    "author": author,
						    "synopsis": synopsis,
						    "publisher": publisher,
						    "genre": genre
						    
						},function(data){
							//$("#eb_success").slideDown(800).delay(800).slideUp();
							//$("#form_editBook").slideUp();
						    //$(".error").slideUp();	
						   	window.location.replace("/Book/Profile/"+isbn);
						    			    
						});
					}
					return false;
				
				});
				
				$(".error a").live('click',function(){
					var t_callNo = $("#ab_callNo").val();
					var t_isbn = $("#ab_isbn").val();
					
					$.post("/Book/addCopy",{
							"isbn": t_isbn,
						    "callNo": t_callNo
						    
						},function(data){
							$("#ab_success").slideDown(800).delay(800).slideUp(); 		
						    $(".error").slideUp();	
						    $("#ab_title").val(""); 						    
							$("#ab_author").val(""); 						    
							$("#ab_publisher").val("");
							$("#ab_callNo").val("")
							$("#ab_isbn").val(""); 						    
							$("#ab_synopsis").val(""); 						    
							$("#ab_image").val("");  			    
						});
				
				});
				
				// -------------------------------------------------  BOOK PAGES EFFECTS --------------------------------------------------------//
				
				$(".book_row").live('click',function(){
					var bookname = $(this).attr("id");
					bookname = bookname.replace("_rows","").replace("_title","");					
					window.location.replace("/Book/Profile/"+bookname);					
				
				});	
				
				// -------------------------------------------------  BOOK RESERVATION CLICKED --------------------------------------------------------//
				
				$(".reservation_row").live('click',function(){
					var bookname = $(this).attr("id");
					bookname = bookname.replace("_row","").replace("_title","");					
					window.location.replace("/Book/Lend/"+bookname);					
				
				});	
				
				// ------------------------------------------------- CANCEL LENDING OF BOOK --------------------------------------------------------//
				
				$(".cancel_loan").live('click',function(){
					var bookname = $("#r_bookisbn").html();					
					window.location.replace("/Book/Profile/"+bookname);					
				
				});	
				
				// ------------------------------------------------- CANCEL LENDING OF BOOK --------------------------------------------------------//
				
				$("#reserveBook").click(function(){
					var bookid = $("#book_id").val();
					var userid = $("#user_id").val();
					var isbn = $("#res_isbn").val();
					$.post("/Book/reserveBook",{
							"book_ID": bookid,
						    "user_ID": userid						    
					},function(data){ 			    
					});
					window.location.replace("/Book/Profile/"+isbn);					
				
				});	
				
				
				
				// -------------------------------------------------  LEND BOOK --------------------------------------------------------//
				
				$(".save_loan").live('click',function(){
					var bookname = $("#r_bookisbn").html();	
					var startdate = $("#borrowstart").val();			
					var enddate= $("#borrowend").val();
					var bookid = $("#r_bookid").val();
					var reservationid = $("#r_reservationid").val();
					var userid = $("#r_userid").val();
					var startdatetrue = new Date(startdate);
					var enddatetrue = new Date(enddate);
					if (startdate=="" || enddate =="")
					{
						$("#r_copy_error").slideUp();	
						$("#r_enddate_error").slideUp();
						$("#r_missing_error").slideDown();					
					}	
					else if(startdatetrue==null || enddatetrue==null)
					{
						$("#r_copy_error").slideUp();
						$("#r_enddate_error").slideUp();
						$("#r_missing_error").slideDown();
					}
					else if (startdatetrue>enddatetrue)
					{
						$("#r_missing_error").slideUp();
						$("#r_copy_error").slideUp();
						$("#r_enddate_error").slideDown();
					}
					else
					{
						$.post("/Book/lendBook",{
							"startdate": startdate,
						    "enddate": enddate,
							"book_ID": bookid,
						    "user_ID": userid,
						    "reservation_ID" : reservationid
						    
						},function(data){
							if (data.toString() == "False")
							{
								$("#r_missing_error").slideUp();
								$("#r_enddate_error").slideUp();
								$("#r_copy_error").slideDown();
							} 
							else 
								window.location.replace("/Book/Profile/"+bookname);			    
						});
					}				
				
				});				
				
			
				
				
});